//顺序表接口的实现

import java.util.Arrays;

public class SeqList {
    private int[] elem; //数组用来存放顺序表元素
    private int usedSize; //数组中的有效元素个数
    private static final int DEFAULT_SIZE = 10; //默认开辟空间的大小为10

    public SeqList(){
        this.elem = new int[DEFAULT_SIZE];
    }

    //打印顺序表
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println(); //换行
    }

    //新增元素，默认在数组最后新增
    public void add(int data){
        //1.先检查数组是否满了
        if(isFull()){
            //2.如果已经满了，则需要扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //3.进行新增
        this.elem[this.usedSize++] = data; //4.usedSize++
    }

    public boolean isFull(){
        return size() >= this.elem.length; //因为返回值是boolean，所以如果>=，则直接返回true
    }

    //在pos位置新增元素
    //如果pos的位置不合法，则会抛出异常
    public void add(int pos, int data) throws PosWrongfulException{
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if(pos < 0 || pos > this.usedSize){ //不可以间隔插入元素
//            System.out.println("位置不合法");
            throw new PosWrongfulException("位置不合法");
        }
        //运行到这里说明pos位置已经合法了
        //移动数据，让出pos位置,pos位置也要移走元素
        for (int i = this.usedSize - 1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        //在pos位置新增元素
        this.elem[pos] = data;
        usedSize++;
    }

    //判定是否包含某个元素
    public boolean contains(int toFind){
        for (int i = 0; i < size(); i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    public int indexOf(int toFind){
        for (int i = 0; i < size(); i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    //获取pos位置的元素
    public int get(int pos){
        if(isEmpty()){
            throw new EmptyException("当前顺序表为空！");
        }
        if (pos < 0 || pos >= this.usedSize){
            throw new PosWrongfulException("位置不合法");
        }
        //获取元素
        return this.elem[pos];
    }

    public boolean isEmpty(){
        return size() == 0;
    }

    //给pos位置的元素设为value
    public void set(int pos, int value){
        if(isEmpty()){
            throw new EmptyException("当前顺序表为空！");
        }
        if (pos < 0 || pos >= this.usedSize){
            throw new PosWrongfulException("获取的pos位置不合法");
        }
        //更新元素
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove){
        if(isEmpty()){
            throw new EmptyException("当前顺序表为空！");
        }
        int index = indexOf(toRemove); //获取关键字的小标
        if(index == -1){
            System.out.println("没有这个数字");
            return;
        }
        //运行到这里表示有这个关键字
        for (int i = index; i < size() - 1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        usedSize--;
    }
    //获取顺序表长度
    public int size(){
        return usedSize;
    }
    //清空顺序表
    public void clear(){
        this.usedSize = 0;
    }

    public static void main(String[] args) {
        SeqList seqList = new SeqList();
        seqList.add(1);
        seqList.add(2);
        seqList.add(3);
        seqList.add(4);
        seqList.add(5);
        seqList.display();
        System.out.println("==========================");
        try {
            seqList.add(1,10);
        }catch (PosWrongfulException e){
            e.printStackTrace();
        }
        System.out.println(seqList.contains(10));
        System.out.println(seqList.contains(100));
        System.out.println(seqList.indexOf(10));
        System.out.println(seqList.indexOf(100));
        seqList.display();
        System.out.println("==========================");
        try {
            System.out.println(seqList.get(1));
        }catch (PosWrongfulException e) {
            e.printStackTrace();
        }
        seqList.set(0,99);
        seqList.display();
        System.out.println("==========================");
        seqList.remove(10);
        seqList.display();
        System.out.println("==========================");
        seqList.clear();
        seqList.display();
        seqList.add(19999);
        seqList.display();
    }
}
